package com.sbs.gaoxinqutoilet.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;

import com.sbs.gaoxinqutoilet.R;
import com.sbs.gaoxinqutoilet.base.BaseScanActivity;
import com.sbs.gaoxinqutoilet.interfaces.Constant;
import com.sbs.gaoxinqutoilet.interfaces.MyWeakReferenceHandler;
import com.sbs.gaoxinqutoilet.utils.SPUtil;

/**
 * Created by 聪明一只哈 on 2018/6/12.
 * Time: 2018/6/12  15:18
 */

public class SplashActivity extends BaseScanActivity {

    private ImageView iv;

    //消息码
    private static final int ANIMATION_END_INDEX = 0;//动画结束
    private static final int INTENT_LOGIN_INDEX = 1;//去登录页面
    private static final int INTENT_LOGIN_NO_OPT_INDEX = 3;
    private static final int INTENT_MAIN_INDEX = 2;//去主页

    private boolean isAnimationEnd;//记录动画是否结束的状态
    private int loginedState; //消息码


    //这里没有用到,要求每次请求都要跳转到登录页
    MyWeakReferenceHandler<SplashActivity> handler = new MyWeakReferenceHandler<SplashActivity>(SplashActivity.this) {
        @Override
        public void handleMessage(Message msg, SplashActivity splashActivity) {
            switch (msg.what) {
                case ANIMATION_END_INDEX:
//                    isAnimationEnd = true;
//                    if (loginedState == INTENT_MAIN_INDEX) { //登录过  直接访问
//                        isAnimationEnd = false;
//                        intentMainAc();
//                    }
//                    if (loginedState == INTENT_LOGIN_INDEX) { // 进入登录
//                        isAnimationEnd = false;
                        //重置过 需要重新输入验证码  显示验证码
//                        SPUtil.putString(Constant.KEY_ONELOGIN, "");
                        intentLoginAc();
//                    }
                    break;
                case INTENT_LOGIN_INDEX:
                    loginedState = INTENT_LOGIN_INDEX;
                    if (isAnimationEnd) {
                        loginedState = -1;

                        SPUtil.putString(Constant.KEY_ONELOGIN, "");
                        intentLoginAc();
                    }
                    break;

                case INTENT_LOGIN_NO_OPT_INDEX:
                   // intentLoginAc();
                    break;
                case INTENT_MAIN_INDEX:
                    loginedState = INTENT_MAIN_INDEX;
                    if (isAnimationEnd) {
                        intentMainAc();
                        loginedState = -1;
                    }
                    break;

                default:
            }
        }
    };
    private String user_name;
    private String user_pass;


    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_splash);
    }

    @Override
    protected void initView() {
        setSwipeBackEnable(false);

        iv = (ImageView) this.findViewById(R.id.logo);
        //iv.setImageResource(R.drawable.splashb);
        iv.setBackgroundResource(R.drawable.splashb);//app-0515-11-59-54
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.2f, 1.0f);
        alphaAnimation.setDuration(5000);

        iv.startAnimation(alphaAnimation);

        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.i("donghua", "onAnimationStart: start");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.i("donghua", "onAnimationStart: end");
                intentLoginAc();

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });

    }

    @Override
    protected void initLogic() {

    }

    private void intentMainAc() {

        Intent intent = new Intent();
        intent.setClass(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
        finish();
    }

    private void intentLoginAc() {
        Intent intent = new Intent();
        intent.setClass(SplashActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }
}
